package qiyu;

import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Data;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors;

public class GroupStream {
    public static List<Student> students = Arrays.asList(
             new Student(11, "张三", "四川", 23, 175),
            new Student(12, "李四", "四川", 38, 168),
            new Student(13, "嬴政", "北京", 26, 185),
            new Student(14, "荆轲", "湖南", 17, 164),
            new Student(15, "高渐离", "陕西", 26, 194),
            new Student(16, "盖聂", "云南", 17, 184),
            new Student(17, "刘邦", "北京", 23, 165),
            new Student(18, "王五", "四川", 38, 98),
            new Student(19, "王阳明", "四川", 50, 123)

    );

    /**
     *分组求各个地方身高最高的学生
     * https://blog.csdn.net/mybook201314/article/details/115284918
     */
    public static void groupByMostHeight(){
        Map<String, Optional<Student>> collect = students.stream().collect(Collectors.groupingBy(Student::getAddr,
                Collectors.mapping(a -> a, Collectors.maxBy((a, b) -> a.getHeight() - b.getHeight()))));
        collect.forEach((k,v)-> System.out.println(k+":"+v.get()));
    }

    @Data
    @AllArgsConstructor
    public static class Student{
        private Integer id;
        private String name;
        private String addr;
        private Integer age;
        @ApiModelProperty("身高")
        private Integer height;
    }
    public static void main(String[] args) {
            groupByMostHeight();
    }
}
